<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28
 * Time: 0:08
 */

namespace Organize\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class RoleResources extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name'=>$this->name,
            'describe' => $this->describe,
            'is_default' => $this->is_default,

            /** 判断是否预加载权限，并且仅在访问角色详情时让 permission 字段出现 */
            $this->mergeWhen(
                $this->whenLoaded('permissions')
                && (strstr(request()->url(),route('organize::roles.index'))
                    || strstr(request()->url(),route('admin::organizes_roles.index'))),
                ['permissions' => $this->getPermissions()]),
        ];
    }
}